package com.antsing.client;

import com.antsing.entity.ChannelProp;
import com.antsing.entity.Constant;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;

import java.util.Date;

public class ClientHandler1  extends ChannelInboundHandlerAdapter {
   /* @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Attribute<ChannelProp> attr = ctx.attr(Constant.NETTY_CHANNEL_KEY);
        ChannelProp channelProp = attr.get();
        if(channelProp == null){
            channelProp = new ChannelProp("antsing_active1",new Date());
            attr.setIfAbsent(channelProp);
        }else {
            System.out.println("channelActive1 attributeMap 中是有值的");
            System.out.println(channelProp.getName() + "=======" + channelProp.getCreateDate());
        }
        System.out.println("ClientHandler1 Active");
        ctx.fireChannelActive();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Attribute<ChannelProp> attr = ctx.attr(Constant.NETTY_CHANNEL_KEY);
        ChannelProp channelProp = attr.get();
        if(channelProp == null){
            channelProp = new ChannelProp("antsing_read1",new Date());
            attr.setIfAbsent(channelProp);
        }else {
            System.out.println("channelRead1 attributeMap 中是有值的");
            System.out.println(channelProp.getName() + "=======" + channelProp.getCreateDate());
        }
        System.out.println("ClientHandler1 read");
        ctx.fireChannelActive();
    }*/
   @Override
   public void channelActive(ChannelHandlerContext ctx) throws Exception {
       Attribute<ChannelProp> attr = ctx.channel().attr(Constant.NETTY_CHANNEL_KEY);
       ChannelProp channelProp = attr.get();
       if(channelProp == null){
           channelProp = new ChannelProp("antsing_active1,hashcode:"+this.hashCode(),new Date());
           attr.setIfAbsent(channelProp);
       }else {
           System.out.println("channelActive1 attributeMap 中是有值的");
           System.out.println(channelProp.getName() + "=======" + channelProp.getCreateDate());
       }
       System.out.println("ClientHandler1 Active");
       ctx.fireChannelActive();
   }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Attribute<ChannelProp> attr = ctx.channel().attr(Constant.NETTY_CHANNEL_KEY);
        ChannelProp channelProp = attr.get();
        if(channelProp == null){
            channelProp = new ChannelProp("antsing_read1",new Date());
            attr.setIfAbsent(channelProp);
        }else {
            System.out.println("channelRead1 attributeMap 中是有值的");
            System.out.println(channelProp.getName() + "=======" + channelProp.getCreateDate());
        }
        System.out.println("ClientHandler1 read");
        ctx.fireChannelActive();
    }
}
